//
//  OCSSOverBookCell.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/15.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit

protocol OCSSOverBookCellDelegate {
    func addButtonDidClicked(button:UIButton, count:NSInteger, cell:OCSSOverBookCell,model:OCSSOverBookModel) -> Void;
    func decButtonDidClicked(button:UIButton, count:NSInteger, cell:OCSSOverBookCell,model:OCSSOverBookModel) -> Void;
}

class OCSSOverBookCell: UITableViewCell {

    @IBOutlet weak var iconImg: UIImageView!
    @IBOutlet weak var nameLab: UILabel!
    @IBOutlet weak var shopNameLab: UILabel!
    @IBOutlet weak var priceLab: UILabel!
    @IBOutlet weak var countLab: UILabel!
    @IBOutlet weak var decBtn: UIButton!
    @IBOutlet weak var addBtn: UIButton!
    
    var model:OCSSOverBookModel? = nil{
        didSet{
            nameLab.text = model?.ProductName;
            shopNameLab.text = model?.ServiceProviderName;
            priceLab.text = String(format: "￥%.2f", (model?.SalePrice)!);
            countLab.text = String(format: "%d", (model?.Quantity)!);
            if (model?.Quantity)! <= 0 {
                decBtn.isHidden = true;
                countLab.isHidden = true;
            }else{
                decBtn.isHidden = false;
                countLab.isHidden = false;
            }
        }
    }
    
    
    var delegate:OCSSOverBookCellDelegate?;
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    @IBAction func addButtonEvent(_ sender: UIButton) {
        let countStr = countLab.text! as NSString;
        var count = countStr.intValue;
        count = count + 1;
        
        delegate?.addButtonDidClicked(button: sender, count: NSInteger(count), cell: self,model: model!);
    }
    @IBAction func decButtonEvent(_ sender: UIButton) {
        let countStr = countLab.text! as NSString;
        var count = countStr.intValue;
        count = count - 1;
    
        delegate?.decButtonDidClicked(button: sender, count: NSInteger(count), cell: self,model: model!);
    }
    
}
